package org.br.erp.base.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.br.erp.base.entity.Basequeryreport;
import org.br.erp.base.entity.Basequeryreportitem;
import org.br.erp.base.entity.Material;
import org.br.erp.base.mapper.BasequeryreportMapper;
import org.br.erp.base.mapper.BasequeryreportitemMapper;
import org.br.erp.base.service.IBasequeryreportitemService;
import org.br.erp.base.service.IMaterialService;
import org.jeecg.common.util.RedisUtil;
import org.springframework.stereotype.Service;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Description: 可用库存详情查询明细
 * @Author: jeecg-boot
 * @Date:   2023-06-11
 * @Version: V1.0
 */
@Service
public class BasequeryreportitemServiceImpl extends ServiceImpl<BasequeryreportitemMapper, Basequeryreportitem> implements IBasequeryreportitemService {
	
	@Autowired
	private BasequeryreportitemMapper basequeryreportitemMapper;

	@Autowired
	private BasequeryreportMapper basequeryreportMapper;

	@Autowired
	private IMaterialService materialService;

	@Autowired
	private RedisUtil redisUtil;

	@Override
	public List<Basequeryreportitem> selectByMainId(String mainId) {
		return basequeryreportitemMapper.selectByMainId(mainId);
	}

	@Override
	public JSONArray selectbymainid(String mainid) {
		JSONArray res=new JSONArray();
		Basequeryreport basequeryreport = basequeryreportMapper.selectById(mainid);
		if(basequeryreport == null){
			return null;
		}
		String billtypename = basequeryreport.getBilltypename();
		String direction = basequeryreport.getDirection();
		String billtypecode = basequeryreport.getBilltypecode();
		List<Basequeryreportitem> basequeryreportitems = basequeryreportitemMapper.selectByMainId(mainid);
		if(basequeryreportitems!=null && basequeryreportitems.size()>0){
			for (Basequeryreportitem basequeryreportitem : basequeryreportitems) {
				JSONObject curRes=new JSONObject();
				curRes.put("billtypename",billtypename);
				curRes.put("billtypecode",billtypecode);
				curRes.put("direction",direction);
				curRes.put("billcode",basequeryreportitem.getBillcode());
				curRes.put("leftqty",basequeryreportitem.getLeftqty());
				curRes.put("qty",basequeryreportitem.getQty());
				curRes.put("subqty",basequeryreportitem.getSubqty());
				if(redisUtil.hasKey(basequeryreportitem.getMaterialid()+"_materialcode")){
					String materialcode = redisUtil.get(basequeryreportitem.getMaterialid() + "_materialcode").toString();
					String materialname = redisUtil.get(basequeryreportitem.getMaterialid() + "_materialname").toString();
					curRes.put("materialcode",materialcode);
					curRes.put("materialname",materialname);
				}else{
					Material materialbyMaterialId = materialService.getMaterialbyMaterialId(basequeryreportitem.getMaterialid());
					if(materialbyMaterialId!=null){
						curRes.put("materialcode",materialbyMaterialId.getMaterialcode());
						curRes.put("materialname",materialbyMaterialId.getMaterialname());
						redisUtil.set(materialbyMaterialId.getId()+"_materialcode",materialbyMaterialId.getMaterialcode(),5);
						redisUtil.set(materialbyMaterialId.getId()+"_materialname",materialbyMaterialId.getMaterialname(),5);
					}
				}
				res.add(curRes);
			}
		}
		return res;
	}
}
